﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using EnCor.ObjectBuilder;

namespace EnCor.Security
{
    [Assembler(typeof(WindowsAuthenticationProviderAssembler))]
    public class WindowsAuthenticationProviderConfig : AuthenticationProviderConfig
    {
        private const string STR_Group = "group";
        [ConfigurationProperty(STR_Group)]
        public string Group
        {
            get
            {
                return (string)this[STR_Group];
            }
        }
    }

    public class WindowsAuthenticationProviderAssembler : AuthenticationProviderAssembler
    {

        public override IAuthenticationProvider Assemble(IBuilderContext context,
            AuthenticationProviderConfig objectConfiguration)
        {
            WindowsAuthenticationProviderConfig config = objectConfiguration as WindowsAuthenticationProviderConfig;
            if (config == null)
            {
                throw new ConfigurationErrorsException();
            }
            List<string> groups = new List<string>();
            foreach ( string groupName in config.Group.Split(',') )
            {
                groups.Add( groupName.Trim() );
            }

            WindowsAuthenticationProvider provider = new WindowsAuthenticationProvider(groups.ToArray());
            return provider;
        }
    }
}
